package com.sky.service.impl;

import com.fasterxml.jackson.core.sym.NameN;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 苏成博
 * @date 2023-02-15 16:07
 */
@Service
public class WorkSpaceServiceImpl implements WorkspaceService {
    private static final Integer ENABLE=1;
    private static final Integer DISABLE=0;
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO getTodayBusinessData(LocalDateTime localTimeMin,LocalDateTime localTimeMax) {
        LocalDateTime localDateTimeMin = localTimeMin;
        LocalDateTime localDateTimeNow = localTimeMax;
        //有效订单数
        Map<String, Object> map = new HashMap<>();
        map.put("begin", localDateTimeMin);
        map.put("end", localDateTimeNow);
        map.put("status", Orders.COMPLETED);
        Double validOrderCount = Double.valueOf(orderMapper.findCompletedOrders(map));
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;
        //查询今天所有的订单
        Double totalOrders = Double.valueOf(orderMapper.findTotalOrders(map));
        totalOrders = totalOrders == null ? 0 : totalOrders;
        //计算订单完成率
        Double orderCompletionRate = validOrderCount / totalOrders;
        orderCompletionRate = orderCompletionRate == null ? 0 : orderCompletionRate;
        //查询今天的营业额
        Double turnoverData = orderMapper.getTurnoverData(map);
        turnoverData = turnoverData == null ? 0 : turnoverData;
        //计算平均客单价
        Double unitPrice = turnoverData / totalOrders;
        unitPrice = unitPrice == null ? 0 : unitPrice;
        //查询新增用户数
        Integer newUser = userMapper.findNewUser(localDateTimeMin, localDateTimeNow);
        return BusinessDataVO.builder()
                .turnover(turnoverData)
                .newUsers(newUser)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount.intValue())
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public OrderOverViewVO getTodayOverviewOrders() {
        LocalDate now = LocalDate.now();
        LocalDateTime localDateTimeMin = now.atTime(LocalTime.MIN);
        LocalDateTime localDateTimeNow = LocalDateTime.now();
        Map<String, Object> map = new HashMap<>();
        map.put("begin", localDateTimeMin);
        map.put("end", localDateTimeNow);
        map.put("status", Orders.TO_BE_CONFIRMED);//查询待接单数量
        Integer waitingOrders = orderMapper.findCompletedOrders(map);
        map.put("status", Orders.CONFIRMED);//带派送
        Integer deliveredOrders = orderMapper.findCompletedOrders(map);
        map.put("status",Orders.COMPLETED);//已完成
        Integer completedOrders = orderMapper.findCompletedOrders(map);
        map.put("status",Orders.CANCELLED);//已取消
        Integer cancelledOrders = orderMapper.findCompletedOrders(map);
        Integer allOrders = orderMapper.findTotalOrders(map);//全部订单

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .build();
    }

    @Override
    public DishOverViewVO getTodayOverviewDishes() {
        Integer sold=dishMapper.findDishByStatus(ENABLE);
        Integer discontinued=dishMapper.findDishByStatus(DISABLE);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getTodayOverviewSetmeals() {
        Integer sold =setmealMapper.getTodayOverviewSetmeals(ENABLE);
        Integer discontinued =setmealMapper.getTodayOverviewSetmeals(DISABLE);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
